/**
 * 工程名：COMMON
 * 包名：com.mt.common.safe
 * 文件名：Permission.java
 */
package com.mt.system.domain;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.mt.component.domain.Common;
import com.mt.utils.StringUtils;



/**
 * 类名称：Permission <br>
 * 类描述：系统权限 <br>
 * 创建人： 马腾 <br>
 * 创建时间：2012-10-18 下午11:31:32 <br>
 */
@Entity
@Table(name="CFG_SYS_PERMISSION")
public class Permission extends Common implements Serializable {
	
	private static final long serialVersionUID = 140205489236426349L;
	
	private String mark;//权限标记
	private Integer sn;//权限排序
	private Permission parent;//父权限
	private String desc;//权限的描述
	private Menu menu;//模块
	private Funct funct;//功能
	private Collection<Permission> children;//子权限
	private boolean selected = false;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="MENU_ID")
	public Menu getMenu() {
		return menu;
	}
	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	
	@Column(name = "PSM_DESC",length = 32)
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="PARENT_ID")
	public Permission getParent() {
		return parent;
	}
	public void setParent(Permission parent) {
		this.parent = parent;
	}
	
	@Column(name = "PSM_SN",length = 4)
	public Integer getSn() {
		return sn;
	}
	public void setSn(Integer sn) {
		this.sn = sn;
	}
	
	@Column(name = "PSM_MARK",length = 64 , unique = true)
	public String getMark() {
		return mark;
	}
	public void setMark(String mark) {
		this.mark = mark;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="FUNCTION_ID")
	public Funct getFunct() {
		return funct;
	}
	public void setFunct(Funct funct) {
		this.funct = funct;
	}
	
	@OneToMany(mappedBy="parent")
	public Collection<Permission> getChildren() {
		return children;
	}
	public void setChildren(Collection<Permission> children) {
		this.children = children;
	}
	
	@Transient
	public boolean getSelected() {
		return selected;
	}
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Permission){
			Permission permission = (Permission)obj;
			if(StringUtils.isEmpty(permission.getId())) return false;
			return permission.getId().equals(getId());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return getId()==null ? 0 : getId().hashCode();
	}
	
	@Override
	public String toString() {
		return "Permission [desc=" + desc + ", sn=" + sn + ", mark=" + mark;
	}
}
